<?php
namespace app\user\controller;
use think\Controller;
use app\user\common\Base as commonController;
use think\Db;
use think\Session;
use think\Request;
class Manage extends commonController {
	public function _initialize()
	{
		$this -> isLogin();
		$request = Request::instance()->action();
		$this->assign('action',$request);
	}
	public function index () {
		return '用户后台页面';
	}
	public function welcome () {
		$user_id = Session::get('user_id');
		$wx = Db::table('wechat_public')->where('owner_user', $user_id)->find();
		$this->assign('wx',$wx);
		return $this->fetch('manage/welcome');
	}
	public function template () {
		$template = Db::table('template')->select();
		$this->assign('template',$template);
		return $this->fetch('manage/template');
	}
	public function author () {
		$user_id = Session::get('user_id');
		$user = Db::table('user')->where('id', $user_id)->find();
		$this->assign('user',$user);
		return $this->fetch('manage/author');
	}
	public function save_author () {
		$param = input('param.');
		$res = Db::table('user')->where('id', Session::get('user_id'))->update(
			[
				'authorizer_name' => $param['authorizer_name'],
				'authorizer_tel'=>$param['authorizer_tel'],
				'wxpay_account'=>$param['wxpay_account'],
				'wxpay_miyao'=>$param['wxpay_miyao'],
				'appid'=>$param['appid'],
				'appscret'=>$param['appscret']
			]
		);
		if ($res) {
			$this->success('恭喜你，更新成功', url('user/Manage/author'));
		} else {
			$this->error('抱歉啊，保存失败', url('user/Manage/author'));
		}
	}
	public function activity () {
		$user_id = Session::get('user_id');
		$activity = Db::table('activity')->where('by_owner_id',$user_id)->order('id asc')->select();
		$this->assign('activity',$activity);
		return $this->fetch('manage/activity');
	}
	public function activity_add () {
		$template_id = input('param.id');
		$user_id = Session::get('user_id');
		$this->assign('user_id',$user_id);
		$this->assign('template_id',$template_id);
		return $this->fetch('template/template'.$template_id);
	}
	public function activity_list () {
		$template = Db::table('template')->select();
		$this->assign('template',$template);
		$user_id = Session::get('user_id');
		$user = Db::table('user')->where('id', $user_id)->find();
		if ($user['is_open'] == 0) {
			return $this->fetch('public/error');
		} else {
			return $this->fetch('manage/activity_list');
		}
	}
	public function activity_create () {
		$param = input('param.');
		$data = [
			'by_owner_id'=> intval($param['by_owner_id']),
			'by_template_id'=> intval($param['by_template_id']),
			'start_time'=> $param['start_time'], //开始时间
			'end_time'=> $param['end_time'], //结束时间
			'activity_banner'=> $param['activity_banner'], //活动主图
			'activity_title'=> $param['activity_title'], //活动标题 省钱风暴，围观就省钱
			'goods_title'=> $param['goods_title'], //商品标题
			'owner_tel'=> $param['owner_tel'], //商户的联系号码
			'activity_rule'=> $param['activity_rule'], //活动规则
			'activity_introduce'=> $param['activity_introduce'], //关于我们的介绍
			'owner_qrcode'=> $param['owner_qrcode'], //客服微信二维码
			'owner_wx_qrcode'=> $param['owner_wx_qrcode'], //商户公众号二维码
			'max_num'=> intval($param['max_num']), //团购达标人数
			'old_price'=> intval($param['old_price']), //原价
			'new_price'=> intval($param['new_price']), //现价
			'max_kan_num'=> intval($param['max_kan_num']), //每次最多砍
			'create_time'=>time()
		];
		$res = Db::table('activity')->insert($data);
		$store = $param['store'];
		$num = count($store);
		for ($i = 0; $i < $num; $i++) {
			$store[$i]['by_activity'] = 1;
		}
		if ($res) {
			Db::name('activity_store')->insertAll($store);
			return [
				'success'=>true,
				'msg'=>'创建成功'
			];
		} else {
			return [
				'success'=>false,
				'msg'=>'创建失败'
			];
		}
	}
	public function activity_edit() {
		$template_id = input('param.id');
		$user_id = Session::get('user_id');
		$this->assign('user_id',$user_id);
		$this->assign('template_id',$template_id);
		return $this->fetch('template/template'.$template_id); 
	}
}
	
	
